<template>
  <panel-head :route="route"></panel-head>
  <div>
    <el-button type="primary" size="small" :icon="Plus" @click="open(null)"
      >新增</el-button
    >
    <el-popconfirm
      confirm-button-text="是"
      cancel-button-text="否"
      :icon="InfoFilled"
      icon-color="#626AEF"
      title="确认要删除?"
      @confirm="confirmEvent"
      @cancel="cancelEvent"
    >
      <template #reference>
        <el-button type="danger" size="small" :icon="Delete">删除</el-button>
      </template>
    </el-popconfirm>
  </div>
  <el-table
    style="width: 100%; margin-top: 10px"
    :data="tableData.list"
    @selection-change="handleSelectChange"
  >
    <el-table-column type="selection" width="55"> </el-table-column>
    <el-table-column prop="id" label="id" />
    <el-table-column prop="name" label="昵称" />
    <el-table-column prop="name" label="头像">
      <template #default="scope">
        <el-image
          prop="avatar"
          style="width: 50px; height: 50px"
          :src="scope.row.avatar"
        >
        </el-image>
      </template>
    </el-table-column>
    <el-table-column prop="sex" label="性别">
      <template #default="scope">
        {{ scope.row.sex == 1 ? "男" : "女" }}
      </template>
    </el-table-column>

    <el-table-column prop="mobile" label="手机号" />
    <el-table-column prop="active" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.active ? 'success' : 'danger'">{{
          scope.row.active ? "正常" : "失效"
        }}</el-tag>
      </template>
    </el-table-column>
    <!-- 创建时间 -->
    <el-table-column label="创建时间">
      <template #default="scope">
        <div class="flex-box">
          <el-icon><Clock /></el-icon>
          <span>{{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination
      v-model:current-page="paginationData.pageNum"
      :page-size="paginationData.pageSize"
      :size="small"
      :background="false"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <el-dialog
    v-model="dialogFromVisible"
    width="500px"
    title="添加陪护"
    :before-close="dialogBeforeClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      label-width="100px"
      label-position="left"
      :rules="rules"
    >
      <!-- 隐藏的id  用于 编辑 -->
      <el-form-item prop="id" v-show="false">
        <el-input v-model="form.id" placeholder=""></el-input>
      </el-form-item>

      <el-form-item label="昵称" prop="name">
        <el-input v-model="form.name" placeholder="请输入昵称" />
      </el-form-item>

      <el-form-item label="头像" prop="avatar">
        <el-button
          v-if="!form.avatar"
          type="primary"
          @click="dialogImgVisible = true"
          >点击上传</el-button
        >
        <el-image
          v-else
          style="width: 100px; height: 100px"
          :src="form.avatar"
          :fit="fit"
        ></el-image>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option label="男" value="1" />
          <el-option label="女" value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="年龄" prop="age">
        <el-input-number
          v-model="form.age"
          @change="inputChange"
          :min="18"
          :max="50"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>

      <el-form-item label="是否生效" prop="active">
        <el-radio-group v-model="form.active">
          <el-radio :value="0" label="失效"></el-radio>
          <el-radio :value="1" label="生效"></el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-foorter">
        <el-button type="primary" @click="confirm(formRef)">确 定</el-button>
      </div>
    </template>
    <div></div>
  </el-dialog>
  <el-dialog
    v-model="dialogImgVisible"
    title="选择图片"
    width="680px"
    :visible.sync="dialogVisible"
    :before-close="dialogBeforeClose"
  >
    <div class="image-list">
      <div
        v-for="(item, index) in fileList"
        :key="item.name"
        class="img-box"
        @click="selectIndex = index"
      >
        <!-- 选中 -->
        <div v-if="selectIndex === index" class="select">
          <el-icon><CircleCheck /></el-icon>
        </div>
        <el-image
          style="width: 148px; height: 148px"
          :src="item.url"
          :fit="fit"
        ></el-image>
      </div>
    </div>
    <template #footer>
      <div class="dialog-foorter">
        <el-button @click="dialogImgVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmImage">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { nextTick, onMounted, reactive, ref } from "vue";
import {
  photoList,
  companion,
  companionList,
  deleteCompanion,
} from "../../../api";
import { CircleCheck, Delete, InfoFilled, Plus } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import dayjs from "dayjs";
import { useRoute } from "vue-router";
const route = useRoute();
onMounted(() => {
  photoList().then(({ data }) => {
    fileList.value = data;
  });
  getListData();
});
// 控制弹窗显示隐藏
const dialogFromVisible = ref(false);
// 选择头像 弹窗
const dialogImgVisible = ref(false);
const fileList = ref([]);
const selectIndex = ref(0);

const dialogBeforeClose = () => {
  dialogFromVisible.value = false;
  // 重置表单
  formRef.value.resetFields();
};
const formRef = ref();
const form = reactive({
  id: "",
  mobile: "",
  active: 1,
  age: 28,
  avatar: "",
  name: "",
  sex: "",
});

const rules = reactive({
  name: [{ required: true, trigger: "blur", message: "请填写昵称" }],
  avatar: [{ required: true, message: "请选择头像" }],
  sex: [{ required: true, trigger: "change", message: "请选择性别" }],
  mobile: [{ required: true, trigger: "blur", message: "请填写手机号" }],
});
// 打开弹窗
const open = (rowData = {}) => {
  dialogFromVisible.value = true;
  nextTick(() => {
    if (rowData) {
      Object.assign(form, {
        id: rowData.id,
        avatar: rowData.avatar,
        sex: rowData.sex,
        mobile: rowData.mobile,
        name: rowData.name,
        permissions_id: rowData.permissions_id,
      });
    }
  });
};
// 表单提交
const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      companion(form).then((data) => {
        if (data.code === 10000) {
          ElMessage.success("成功");
          getListData();
          dialogBeforeClose();
        } else {
          ElMessage.error(data.message);
        }
      });
    } else {
      console.log("error submit", fields);
    }
  });
};
// 确认上传图片
const confirmImage = () => {
  form.avatar = fileList.value[selectIndex.value].url;
  dialogImgVisible.value = false;
};

// 列表数据
const tableData = reactive({
  list: [],
  total: 0,
});
const paginationData = reactive({
  pageNum: 1,
  pageSize: 10,
});
const getListData = () => {
  companionList(paginationData).then((data) => {
    const { list, total } = data.data;
    list.forEach((item) => {
      item.create_time = dayjs(item.create_time).format("YYYY-MM-DD");
    });
    tableData.list = list;
    tableData.total = total;
  });
};
const selectTabData = ref([]);
const handleSelectChange = (val) => {
  selectTabData.value = val.map((item) => ({ id: item.id }));
};
const confirmEvent = () => {
  // 没有选择数据
  if (!selectTabData.value.length) {
    ElMessage.warning("请选择至少一项数据");
    return;
  }
  deleteCompanion({ id: selectTabData.value }).then((data) => {
    if (data.code === 10000) {
      ElMessage.success("删除成功");
      getListData();
    } else {
      ElMessage.success("删除失败");
    }
  });
};
const cancelEvent = () => {};
const handleSizeChange = (val) => {
  paginationData.pageSize = val;
  getListData();
};
const handleCurrentChange = (val) => {
  paginationData.pageNum = val;
  getListData();
};
</script>
<style scoped lang="less">
.flex-box {
  display: flex;
  align-items: center;
  span {
    margin-left: 10px;
  }
}
.btns {
  padding: 10px 0 10px 10px;
  background-color: #fff;
}
.image-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .img-box {
    position: relative;
    .select {
      position: absolute;
      left: 0px;
      top: 0px;
      width: 24px;
      height: 24px;
      background-color: #67c23a;
      z-index: 999;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .el-image {
    margin-right: 10px;
    margin-bottom: 10px;
  }
}
</style>
